package com.medical.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.medical.system.model.SysRole;
import com.medical.system.service.SysRoleService;
import com.medical.system.common.Result;
import com.medical.system.vo.AssginRoleVo;
import com.medical.system.vo.SysRoleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

@Api(tags = "角色控制器")
@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @ApiOperation("角色分页查询+模糊")
    @GetMapping("/{page}/{limit}")
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    public Result queryRoleByPage(
            @PathVariable Long page,
            @PathVariable Long limit,
            SysRoleQueryVo roleQueryVo
    ) {
        IPage<SysRole> pageModel = new Page<>(page, limit);
        pageModel = this.sysRoleService.selectPage(pageModel, roleQueryVo);
        return Result.ok(pageModel);
    }

    @ApiOperation("根据id查询")
    @GetMapping("findRoleById/{id}")
    public Result queryRoleById(@PathVariable Long id) {
        SysRole sysRole = this.sysRoleService.getById(id);
        return Result.ok(sysRole);
    }


    @ApiOperation("添加新角色")
    @PostMapping("addRole")
    @PreAuthorize("hasAuthority('bnt.sysRole.add')")
    public Result insert(@RequestBody SysRole sysRole) {
        boolean save = this.sysRoleService.save(sysRole);
        if (save) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation("逻辑删除角色")
    @DeleteMapping("/removeRole/{id}")
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    public Result removeById(@PathVariable @ApiParam("角色ID") Long id) {
        boolean b = this.sysRoleService.removeById(id);
        if (b) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation("批量删除")
    @DeleteMapping("batchRemove")
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    public Result batchRemove(@RequestBody List<Long> ids) {
        boolean isSuccess = this.sysRoleService.removeByIds(ids);
        if (isSuccess) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation("修改角色")
    @PostMapping("updateRole")
    @PreAuthorize("hasAuthority('bnt.sysRole.update')")
    public Result updateRole(@RequestBody SysRole sysRole) {
        boolean isSuccess = this.sysRoleService.updateById(sysRole);
        if (isSuccess) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation(value = "根据用户ID获取角色数据")
    @GetMapping("/toAssign/{userId}")
    public Result toAssign(@PathVariable Long userId) {
        Map<String, Object> roleMap = sysRoleService.getRolesByUserId(userId);
        return Result.ok(roleMap);
    }

    @ApiOperation(value = "给用户分配角色")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssginRoleVo assginRoleVo) {
        sysRoleService.doAssign(assginRoleVo);
        return Result.ok();
    }
}
